import React from 'react';
import { Modal, Form, Checkbox, Row } from 'antd';
import cn from 'classnames';
import { formatMessage } from 'umi';

import { TipBox } from '../../../components';
import { pressTaskAudit, pressTaskAcceptor } from '../../../services';
import { getActionItem } from '../../../utils';
import styles from './index.less';

const PushMsg = ({ form, type }) => {
    const [, setVal] = React.useState('1');
    return (
        <Row type="flex" justify="center">
            <Form layout="inline">
                <Form.Item label={`是否需向${type}人发送催办通知`}>
                    {form.getFieldDecorator('pushMsg', {
                        initialValue: ['1']
                    })(
                        <Checkbox.Group onChange={e => setVal(e)}>
                            <Checkbox value="1">是</Checkbox>
                        </Checkbox.Group>
                    )}
                </Form.Item>
            </Form>
        </Row>
    );
};

export const showPressModal = ({
    form,
    taskId,
    title = '提示',
    type = '',
    employeeName = '',
    operateType = '',
    onOk
}) => {
    // console.log('showPressModal -> taskId', taskId);
    const renderContent = () => {
        if (operateType) {
            const oType = getActionItem(operateType) || '';
            return (
                <TipBox
                    title={
                        <>
                            <div style={{ textAlign: 'center' }}>当前任务已发起{oType}</div>
                            <div style={{ textAlign: 'center' }}>
                                正在待【{employeeName}】{type}中！
                            </div>
                        </>
                    }
                    content={`请待${type}后再发起相应操作!`}
                />
            );
        }
        return (
            <TipBox title={`当前任务正在待【${employeeName}】${type}中！`} content={`请待${type}后再发起相应操作!`} />
        );
    };

    Modal.confirm({
        title,
        content: (
            <>
                {renderContent()}
                <PushMsg form={form} type={type} />
            </>
        ),
        className: cn(styles.confirmModal, styles.hasNoFirstBtn),
        okText: formatMessage({ id: 'btn.form.confirm' }),
        cancelText: formatMessage({ id: 'btn.form.cancel' }),
        onOk: () =>
            new Promise((resolve, reject) => {
                const { pushMsg } = form.getFieldsValue();
                if (pushMsg.length <= 0) {
                    resolve();
                } else {
                    if (type === '审核') {
                        pressTaskAudit(taskId)
                            .then(res => {
                                const { code } = res;
                                if (code === 10000) {
                                    if (onOk) onOk();
                                    resolve();
                                } else {
                                    reject();
                                }
                            })
                            .catch(() => {
                                reject();
                            });
                        return;
                    }
                    pressTaskAcceptor(taskId)
                        .then(res => {
                            const { code } = res;
                            if (code === 10000) {
                                if (onOk) onOk();
                                resolve();
                            } else {
                                reject();
                            }
                        })
                        .catch(() => {
                            reject();
                        });
                }
            })
    });
};
